package com.project.base.baseproject.AppCustoms.SelectImageActivity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.jaeger.library.StatusBarUtil;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.project.base.baseproject.Activity.BaseActivity.BaseActivity;
import com.project.base.baseproject.AppCustoms.Tool.ToastUtil;
import com.project.base.baseproject.R;

import java.io.File;
import java.util.Date;


import static android.content.pm.PackageManager.PERMISSION_GRANTED;

//扫描二维码
public class ScanQcoreActivity extends BaseActivity implements View.OnClickListener {
    //条形码扫描管理器
    private CaptureManager mCaptureManager;
    //条形码扫描视图
    private DecoratedBarcodeView mBarcodeView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.meeting_signin_layout);
        findViewById(R.id.return_btn).setOnClickListener(this);
        //检查权限问题
        //检查有没有拍照权限
        if(ActivityCompat.checkSelfPermission(ScanQcoreActivity.this, Manifest.permission.CAMERA) != PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(ScanQcoreActivity.this,new String[]{Manifest.permission.CAMERA,},5002);
        } else {
            mBarcodeView = (DecoratedBarcodeView)findViewById(R.id.zxing_barcode_scanner);
            mCaptureManager = new CaptureManager(this, mBarcodeView);
            mCaptureManager.initializeFromIntent(getIntent(), null);
            mCaptureManager.decode();
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        mCaptureManager.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        mCaptureManager.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCaptureManager.onDestroy();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mCaptureManager.onSaveInstanceState(outState);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 5002) {
            int resultCode = grantResults[0];
            if(resultCode != PERMISSION_GRANTED) {
                ToastUtil.show(ScanQcoreActivity.this,"没有相机权限");
                finish();
            } else {
                mBarcodeView = (DecoratedBarcodeView)findViewById(R.id.zxing_barcode_scanner);
                mCaptureManager = new CaptureManager(this, mBarcodeView);
                mCaptureManager.initializeFromIntent(getIntent(), null);
                mCaptureManager.decode();
            }
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return mBarcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }
    //点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.return_btn:
                finish();
                break;
            default:
                break;
        }
    }
}
